package com.ccgk.logistics.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ccgk.common.MyCollectionUtils;
import com.ccgk.common.page.PaginationVo;
import com.ccgk.common.web.RequestUtils;
import com.ccgk.logistics.domain.Currency;
import com.ccgk.logistics.domain.Fee;
import com.ccgk.logistics.service.CurrencyService;
import com.ccgk.logistics.service.FeeService;

@Controller
@RequestMapping("/fee")
public class FeeController {
	
	@Autowired
	private FeeService feeService;
	@Autowired
	private CurrencyService currencyService;
	
	/**查询列表*/
	@RequestMapping("/list")
	public String list(HttpServletRequest request){
		int currentPage = RequestUtils.getInt(request,"currentPage",1);
		int pageSize = RequestUtils.getInt(request,"pageSize",PaginationVo.DEFAULT_PAGESIZE);
		
		FeeService.FeeCriteria  criteria = new FeeService.FeeCriteria();
		String name = request.getParameter("name");
		if(StringUtils.isNotBlank(name))
			criteria.nameLike(name);
		PaginationVo<Fee> result = feeService.findList(criteria,currentPage, pageSize);
		request.setAttribute("result",result);
		return "fee/list";
	}
	
	/**获取单个*/
	@RequestMapping("/{id}")
	public String getById(@PathVariable("id")Integer id,Model model){
		Fee fee = feeService.getById(id);
		List<Currency> currencies = currencyService.findAll();
		model.addAttribute("currencies",currencies);
		model.addAttribute("obj", fee);
		return "fee/edit";
	}
	
	/**更新信息*/
	@ResponseBody
	@RequestMapping(value="/{id}",method=RequestMethod.POST)
	public String update(Fee fee){
		Fee _fee  = feeService.getByName(fee.getName());
		if(_fee != null && !_fee.getId().equals(fee.getId()))
			return "{flag:1}";
		feeService.update(fee);
		return "{flag:0}";
	}
	/**新增页*/
	@RequestMapping("/add")
	public String showAdd(Model model){
		
		List<Currency> currencies = currencyService.findAll();
		model.addAttribute("currencies",currencies);
		return "fee/add";
	}
	/**新增*/
	@ResponseBody
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public String add(Fee fee,HttpServletResponse response){
		if(feeService.getByName(fee.getName()) != null){
			return "{flag:1}";
		}
		feeService.add(fee);
		return "{flag:0}";
	}
	/**删除*/
	@ResponseBody
	@RequestMapping("/delete")
	public String delete(@RequestParam("id")String ids,Model model){
		Integer[] idIntArray = MyCollectionUtils.splitToIntArray(ids);
		feeService.batchDelete(idIntArray);
		return "{success:true}";
	}
}
